//
//  SimpleBarChartForSkill.m
//  YouLogReading
//
//  Created by Mohammed Islam on 9/18/13.
//  Copyright (c) 2013 KSI Technology, LLC. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.


#import "SimpleBarChartForSkill.h"

@interface SimpleBarChartForSkill ()
{
    NSMutableArray *XAxisList;
}
@end

@implementation SimpleBarChartForSkill

@synthesize
delegate	= _delegate,
dataSource	= _dataSource;

- (id)initWithFrame:(CGRect)frame
{
    
    if (!(self = [super initWithFrame:frame]))
        return self;
    
	self.animationDuration	= 0;
	self.hasGrids			= YES;
	self.incrementValue		= 10;
	self.barWidth			= 20.0;
	self.barAlpha			= 1.0;
	self.chartBorderColor	= [UIColor blackColor];
	self.gridColor			= [UIColor grayColor];
	self.hasYLabels			= YES;
	self.yLabelFont			= [UIFont fontWithName:@"Helvetica" size:12.0];
	self.yLabelColor		= [UIColor blackColor];
	self.xLabelFont			= [UIFont fontWithName:@"Helvetica" size:12.0];
    self.xBoldLabelFont = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
	self.xLabelColor		= [UIColor blackColor];
	self.xLabelType			= SimpleBarChartForSkillXLabelTypeVerticle;
	self.barTextFont		= [UIFont fontWithName:@"Helvetica" size:12.0];
	self.barTextColor		= [UIColor blackColor];
	self.barTextType		= SimpleBarChartForSkillBarTextTypeTop;
    
	_barPathLayers			= [[NSMutableArray alloc] init];
	_barLabels				= [[NSMutableArray alloc] init];
	_barTexts				= [[NSMutableArray alloc] init];
    
    //Set Color
    self.monthRequirementsBarColor =[UIColor colorWithRed:0.400 green:0.400 blue:0.800 alpha:1.000];
    self.meetRequirementsBarColor=[UIColor colorWithRed:0.400 green:0.800 blue:0.000 alpha:1.000];
    self.legacyRequirementsBarColor =[UIColor colorWithRed:1.000 green:0.600 blue:0.200 alpha:0.920];
    self.barCountTextColor = [UIColor whiteColor];
    //Sa ##############
    _saMonthRequirementsBarHeights= [[NSMutableArray alloc] init];
    _saMeetRequirementsBarHeights= [[NSMutableArray alloc] init];
    _saLegacyRequirementsBarHeights= [[NSMutableArray alloc] init];
    
    //AP ##############
    _apMonthRequirementsBarHeights= [[NSMutableArray alloc] init];
    _apMeetRequirementsBarHeights= [[NSMutableArray alloc] init];
    _apLegacyRequirementsBarHeights= [[NSMutableArray alloc] init];
    
    //p ##############
    _pMonthRequirementsBarHeights= [[NSMutableArray alloc] init];
    _pMeetRequirementsBarHeights= [[NSMutableArray alloc] init];
    _pLegacyRequirementsBarHeights = [[NSMutableArray alloc] init];
	
    
    _LegacyRequirementsbarHeights =[[NSMutableArray alloc] init];
    
	// Grid  ##############
	_gridLayer				= [CALayer layer];
	[self.layer addSublayer:_gridLayer];
    
	_barLayer				= [CALayer layer];
	[self.layer addSublayer:_barLayer];
    
	_borderLayer			= [CALayer layer];
	[self.layer addSublayer:_borderLayer];
    
	_yLabelView				= [[UIView alloc] init];
	_yLabelView.alpha		= 0.0;
	[self addSubview:_yLabelView];
    
	_xLabelView				= [[UIView alloc] init];
	_xLabelView.alpha		= 0.0;
	[self addSubview:_xLabelView];
    
	_barTextView			= [[UIView alloc] init];
	_barTextView.alpha		= 0.0;
	[self addSubview:_barTextView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
    
    return self;
}



- (void)tap:(UIGestureRecognizer *)gr
{
    CGPoint point = [gr locationInView:self];
    CGFloat min = CGRectGetMinY(_borderLayer.bounds);
    CGFloat max = CGRectGetMaxY(_borderLayer.bounds);
    
    for (int i=0; i<XAxisList.count; i++) {
        double x = [[XAxisList objectAtIndex:i] doubleValue];
        if (ABS(point.x - x) > 45.0) continue;
        if (point.y < min) continue;
        if (point.y > max) continue;
        
        [_delegate selectAtItem:i];
        break;
    }
}

- (void)reloadData
{
	if (_dataSource)
	{
		// Collect some data
		_numberOfBars = [_dataSource numberOfBarsInBarChart:self];
        //Sa ##############
        [_saMonthRequirementsBarHeights removeAllObjects];
        [_saMeetRequirementsBarHeights removeAllObjects];
        [_saLegacyRequirementsBarHeights removeAllObjects];
        
        //AP ##############
        [_apMonthRequirementsBarHeights removeAllObjects];
        [_apMeetRequirementsBarHeights removeAllObjects];
        [_apLegacyRequirementsBarHeights removeAllObjects];
        
        //p ##############
        [_pMonthRequirementsBarHeights removeAllObjects];
        [_pMeetRequirementsBarHeights removeAllObjects];
        [_pLegacyRequirementsBarHeights  removeAllObjects];
        
        //##############
        [_LegacyRequirementsbarHeights removeAllObjects];
        
		[_barLabels removeAllObjects];
		[_barTexts removeAllObjects];
        
        _maxSaHeight =[[NSNumber alloc] initWithInt:0];
        _maxApHeight =[[NSNumber alloc] initWithInt:0];
        _maxPHeight =[[NSNumber alloc] initWithInt:0];
        
        _minSaHeight =[[NSNumber alloc] initWithInt:0];
        _minApHeight =[[NSNumber alloc] initWithInt:0];
        _minPHeight =[[NSNumber alloc] initWithInt:0];
        
        
		for (NSInteger i = 0; i < _numberOfBars; i++)
		{
            
            //Sa ##############
            [_saMonthRequirementsBarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForSaMonthRequirementsBarAtIndex:i]]];
            [_saMeetRequirementsBarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForSaMeetRequirementsBarAtIndex:i]]];
            [_saLegacyRequirementsBarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForSaLegacyRequirementsBarAtIndex:i]]];
            
            //AP ##############
            [_apMonthRequirementsBarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForApMonthRequirementsBarAtIndex:i]]];
            [_apMeetRequirementsBarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForApMeetRequirementsBarAtIndex:i]]];
            [_apLegacyRequirementsBarHeights  addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForApLegacyRequirementsBarAtIndex:i]]];
            
            //p ##############
            [_pMonthRequirementsBarHeights  addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForPMonthRequirementsBarAtIndex:i]]];
            [_pMeetRequirementsBarHeights  addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForPMeetRequirementsBarAtIndex:i]]];
            [_pLegacyRequirementsBarHeights   addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForPLegacyRequirementsBarAtIndex:i]]];
            
            //##############
            
            
            [_LegacyRequirementsbarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForSaLegacyRequirementsBarAtIndex:i]]];
            [_LegacyRequirementsbarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForApLegacyRequirementsBarAtIndex:i]]];
            [_LegacyRequirementsbarHeights addObject:[NSNumber numberWithFloat:[_dataSource barChart:self valueForPLegacyRequirementsBarAtIndex:i]]];
            
            //##############
            //Bar名称
			if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:xLabelForBarAtIndex:)])
				[_barLabels addObject:[_dataSource barChart:self xLabelForBarAtIndex:i]];
            
            //设置Bar计数
			if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:textForSaBarAtIndex:)])
				[_barTexts addObject:[_dataSource barChart:self textForSaBarAtIndex:i]];
            if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:textForApBarAtIndex:)])
				[_barTexts addObject:[_dataSource barChart:self textForApBarAtIndex:i]];
            if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:textForPBarAtIndex:)])
				[_barTexts addObject:[_dataSource barChart:self textForPBarAtIndex:i]];
            
            
		}
		
        //########################
        //设置表格最大值是取自哪个Bar
        NSMutableArray *maxHeight;
        NSMutableArray *minHeight;
        
        maxHeight = [[NSMutableArray alloc] init];
        minHeight = [[NSMutableArray alloc] init];
        
        
        
        NSNumber *saMaxHeight = [[NSNumber alloc] initWithFloat:0];
        saMaxHeight =[_saLegacyRequirementsBarHeights  valueForKeyPath:@"@max.self"];
        if(!saMaxHeight){saMaxHeight = [[NSNumber alloc] initWithFloat:0];}
        
        NSNumber *apMaxHeight = [[NSNumber alloc]initWithFloat:0];
        apMaxHeight= [_apLegacyRequirementsBarHeights  valueForKeyPath:@"@max.self"];
        if(!apMaxHeight){apMaxHeight = [[NSNumber alloc] initWithFloat:0];}
        
        NSNumber *pMaxHeight = [[NSNumber alloc]initWithFloat:0];
        pMaxHeight= [_pLegacyRequirementsBarHeights  valueForKeyPath:@"@max.self"];
        if(!pMaxHeight){pMaxHeight = [[NSNumber alloc] initWithFloat:0];}
        
        [maxHeight addObject:saMaxHeight];
        [maxHeight addObject:apMaxHeight];
        [maxHeight addObject:pMaxHeight];
        
        
        NSNumber *saMinHeight = [[NSNumber alloc]initWithFloat:0];
        saMinHeight= [_saLegacyRequirementsBarHeights  valueForKeyPath:@"@min.self"];
        if(!saMinHeight){saMinHeight = [[NSNumber alloc] initWithFloat:0];}
        
        NSNumber *apMinHeight = [[NSNumber alloc]initWithFloat:0];
        apMinHeight= [_apLegacyRequirementsBarHeights  valueForKeyPath:@"@min.self"];
        if(!apMinHeight){apMinHeight = [[NSNumber alloc] initWithFloat:0];}
        
        NSNumber *pMinHeight = [[NSNumber alloc]initWithFloat:0];
        pMinHeight= [_pLegacyRequirementsBarHeights  valueForKeyPath:@"@min.self"];
        if(!pMinHeight){pMinHeight = [[NSNumber alloc] initWithFloat:0];}
        
        [maxHeight addObject:saMinHeight];
        [maxHeight addObject:apMinHeight];
        [maxHeight addObject:pMinHeight];
        
        _maxHeight		=[maxHeight valueForKeyPath:@"@max.self"];
        _minHeight		=[minHeight valueForKeyPath:@"@min.self"];
        
        _maxSaHeight =[_saLegacyRequirementsBarHeights valueForKeyPath:@"@max.self"];
        _maxApHeight =[_apLegacyRequirementsBarHeights valueForKeyPath:@"@max.self"];
        _maxPHeight =[_pLegacyRequirementsBarHeights valueForKeyPath:@"@max.self"];
        
        _minSaHeight =[_saLegacyRequirementsBarHeights valueForKeyPath:@"@min.self"];
        _minApHeight =[_apLegacyRequirementsBarHeights valueForKeyPath:@"@min.self"];
        _minPHeight =[_pLegacyRequirementsBarHeights valueForKeyPath:@"@min.self"];
        //########################
		
		// Round up to the next increment value
		_topValue		= (self.incrementValue - (_maxHeight.integerValue % self.incrementValue)) + _maxHeight.integerValue;
        
        _topSaCountValue = (self.incrementValue - (_maxSaHeight.integerValue % self.incrementValue)) + _maxSaHeight.integerValue;
        _topApCountValue = (self.incrementValue - (_maxApHeight.integerValue % self.incrementValue)) + _maxApHeight.integerValue;
        _topPCountValue = (self.incrementValue - (_maxPHeight.integerValue % self.incrementValue)) + _maxPHeight.integerValue;
        
        //########################
        
        if (_topValue < 5) _topValue = 5;
        
		// Find max height of the x Labels based on the angle of rotation of the text
		switch (self.xLabelType)
		{
			case SimpleBarChartForSkillXLabelTypeVerticle:
			default:
				_xLabelRotation = 90.0;
				break;
                
			case SimpleBarChartForSkillXLabelTypeHorizontal:
				_xLabelRotation = 0.0;
				break;
                
			case SimpleBarChartForSkillXLabelTypeAngled:
				_xLabelRotation = 45.0;
				break;
		}
		
		for (NSString *label in _barLabels)
		{
            NSDictionary *xLabelFontAttributes = @{NSFontAttributeName: self.xLabelFont};
            CGSize labelSize = [label sizeWithAttributes:xLabelFontAttributes];
			CGFloat labelHeightWithAngle = sin(DEGREES_TO_RADIANS(_xLabelRotation)) * labelSize.width;
            
			if (labelSize.height > labelHeightWithAngle)
			{
				_xLabelMaxHeight = (_xLabelMaxHeight > labelSize.height) ? _xLabelMaxHeight : labelSize.height;
			}
			else
			{
				_xLabelMaxHeight = (_xLabelMaxHeight > labelHeightWithAngle) ? _xLabelMaxHeight : labelHeightWithAngle;
			}
		}
        
		// Begin Drawing
		// Set Frames
        NSDictionary *xLabelFontAttributes = @{NSFontAttributeName: self.xLabelFont};
		CGSize yLabelSize		= self.hasYLabels ? [[NSString stringWithFormat:@"%li", (long)_topValue] sizeWithAttributes:xLabelFontAttributes] : CGSizeZero;
		_yLabelView.frame		= CGRectMake(0.0,
											 0.0,
											 self.hasYLabels ? yLabelSize.width + 5.0 : 0.0,
											 self.bounds.size.height);
		
		_xLabelView.frame		= CGRectMake(_yLabelView.frame.origin.x + _yLabelView.frame.size.width,
											 self.bounds.size.height - _xLabelMaxHeight,
											 self.bounds.size.width - (_yLabelView.frame.origin.x + _yLabelView.frame.size.width),
											 _xLabelMaxHeight);
		
		_gridLayer.frame		= CGRectMake(_yLabelView.frame.origin.x + _yLabelView.frame.size.width,
											 0.0,
											 self.bounds.size.width - (_yLabelView.frame.origin.x + _yLabelView.frame.size.width),
											 self.bounds.size.height - (_xLabelMaxHeight > 0.0 ? (_xLabelMaxHeight + 5.0) : 0.0));
		_barLayer.frame			= _gridLayer.frame;
		_borderLayer.frame		= _gridLayer.frame;
		_barTextView.frame		= _gridLayer.frame;
        
		// Draw dem stuff
		[self setupBorders];
		[self drawBorders];
        
		@autoreleasepool {
			[self setupBars];
			[self animateBarAtIndex:0];
		}
        
		if (self.hasGrids)
		{
			[self setupGrid];
			[self drawGrid];
		}
        
		[self setupYAxisLabels];
		[self setupXAxisLabels];
        
		[self setupBarTexts];
	}
}

#pragma mark Borders

- (void)setupBorders
{
	if (_borderPathLayer != nil)
	{
		[_borderPathLayer removeFromSuperlayer];
		_borderPathLayer = nil;
	}
    
	CGPoint bottomLeft 	= CGPointMake(CGRectGetMinX(_borderLayer.bounds), CGRectGetMinY(_borderLayer.bounds));
	CGPoint bottomRight = CGPointMake(CGRectGetMaxX(_borderLayer.bounds), CGRectGetMinY(_borderLayer.bounds));
	CGPoint topLeft		= CGPointMake(CGRectGetMinX(_borderLayer.bounds), CGRectGetMaxY(_borderLayer.bounds));
	CGPoint topRight	= CGPointMake(CGRectGetMaxX(_borderLayer.bounds), CGRectGetMaxY(_borderLayer.bounds));
    
	UIBezierPath *path	= [UIBezierPath bezierPath];
	[path moveToPoint:bottomRight];
	[path addLineToPoint:topRight];
	[path addLineToPoint:topLeft];
	[path addLineToPoint:bottomLeft];
	[path addLineToPoint:bottomRight];
    
	_borderPathLayer					= [CAShapeLayer layer];
	_borderPathLayer.frame				= _borderLayer.bounds;
	_borderPathLayer.bounds				= _borderLayer.bounds;
	_borderPathLayer.geometryFlipped	= YES;
	_borderPathLayer.path				= path.CGPath;
	_borderPathLayer.strokeColor		= self.chartBorderColor.CGColor;
	_borderPathLayer.fillColor			= nil;
	_borderPathLayer.lineWidth			= 1.0f;
	_borderPathLayer.lineJoin			= kCALineJoinBevel;
    
	[_borderLayer addSublayer:_borderPathLayer];
}

- (void)drawBorders
{
	if (self.animationDuration == 0.0)
		return;
	
	[_borderPathLayer removeAllAnimations];
    
    CABasicAnimation *pathAnimation	= [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration			= self.animationDuration;
    pathAnimation.fromValue			= [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue			= [NSNumber numberWithFloat:1.0f];
    [_borderPathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

#pragma mark Bars

- (void)setupBars
{
	// Clear all bars for each drawing
	for (CAShapeLayer *layer in _barPathLayers)
	{
		if (layer != nil)
		{
			[layer removeFromSuperlayer];
		}
	}
	[_barPathLayers removeAllObjects];
    
	int barSpacing = 25;
    CGFloat barHeightRatio	= _barLayer.bounds.size.height / (CGFloat)_topValue;
    //##############################
    //saLegacyRequirementsBarHeights
    
	CGFloat	xPos			= _barLayer.bounds.size.width / (_numberOfBars + 1)-25;
    
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_saLegacyRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_saLegacyRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.legacyRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
		barPathLayer.shadowOffset		= self.barShadowOffset;
		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
		barPathLayer.shadowOpacity		= self.barShadowAlpha;
		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //_apLegacyRequirementsBarHeights
    xPos	=_barLayer.bounds.size.width / (_numberOfBars + 1)-25+ barSpacing*1;
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_apLegacyRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_apLegacyRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.legacyRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
		barPathLayer.shadowOffset		= self.barShadowOffset;
		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
		barPathLayer.shadowOpacity		= self.barShadowAlpha;
		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //_pLegacyRequirementsBarHeights
    xPos	=_barLayer.bounds.size.width / (_numberOfBars + 1)-25+ barSpacing*2;
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_pLegacyRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_pLegacyRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.legacyRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
		barPathLayer.shadowOffset		= self.barShadowOffset;
		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
		barPathLayer.shadowOpacity		= self.barShadowAlpha;
		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //##############################
    //_saMonthRequirementsBarHeights
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25;
    
	for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_saMonthRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.monthRequirementsBarColor;
        //		if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:colorForBarAtIndex:)])
        //			barColor = [_dataSource barChart:self colorForBarAtIndex:i];
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //_apMonthRequirementsBarHeights
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25+barSpacing*1;
    
    
	for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_apMonthRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.monthRequirementsBarColor;
        //		if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:colorForBarAtIndex:)])
        //			barColor = [_dataSource barChart:self colorForBarAtIndex:i];
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //_pMonthRequirementsBarHeights
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25+barSpacing*2;
    
    
	for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_pMonthRequirementsBarHeights  objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.monthRequirementsBarColor;
        //		if (_dataSource && [_dataSource respondsToSelector:@selector(barChart:colorForBarAtIndex:)])
        //			barColor = [_dataSource barChart:self colorForBarAtIndex:i];
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    
    //##############################
    //SA MEET QRE
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25;
    
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_saMeetRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_saMeetRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.meetRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //AP MEET REQ
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25+barSpacing*1;
    
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_apMeetRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_apMeetRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.meetRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    //P MEET REQ
    xPos	= _barLayer.bounds.size.width / (_numberOfBars + 1)-25+barSpacing*2;
    
    for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		CGPoint bottom					= CGPointMake(xPos, _barLayer.bounds.origin.y);
		CGPoint top						= CGPointMake(xPos, ((NSNumber *)[_pMeetRequirementsBarHeights objectAtIndex:i]).floatValue * barHeightRatio);
		xPos							+= _barLayer.bounds.size.width / (_numberOfBars + 1);
        
        if ([[_pMeetRequirementsBarHeights objectAtIndex:i] intValue] < 1) continue;
        
		UIBezierPath *path				= [UIBezierPath bezierPath];
		[path moveToPoint:bottom];
		[path addLineToPoint:top];
        
		UIColor *barColor				= self.meetRequirementsBarColor;
        
		CAShapeLayer *barPathLayer		= [CAShapeLayer layer];
		barPathLayer.frame				= _barLayer.bounds;
		barPathLayer.bounds				= _barLayer.bounds;
		barPathLayer.geometryFlipped	= YES;
		barPathLayer.path				= path.CGPath;
		barPathLayer.strokeColor		= [barColor colorWithAlphaComponent:self.self.barAlpha].CGColor;
		barPathLayer.fillColor			= nil;
		barPathLayer.lineWidth			= self.barWidth;
		barPathLayer.lineJoin			= kCALineJoinBevel;
		barPathLayer.hidden				= YES;
        //		barPathLayer.shadowOffset		= self.barShadowOffset;
        //		barPathLayer.shadowColor		= self.barShadowColor.CGColor;
        //		barPathLayer.shadowOpacity		= self.barShadowAlpha;
        //		barPathLayer.shadowRadius		= self.barShadowRadius;
        
		[_barLayer addSublayer:barPathLayer];
		[_barPathLayers addObject:barPathLayer];
	}
    
    
    //##############################
    
    
    
}

- (void)animateBarAtIndex:(NSInteger)index
{
	if (index >= _barPathLayers.count)
	{
		// Last bar, begin drawing grids
		[self displayAxisLabels];
		return;
	}
    
	__block NSInteger i				= index + 1;
	__weak SimpleBarChartForSkill *weakSelf = self;
	[CATransaction begin];
	[CATransaction setAnimationDuration:(self.animationDuration / (CGFloat)_barPathLayers.count)];
	[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
	[CATransaction setCompletionBlock:^{
		[weakSelf animateBarAtIndex:i];
	}];
    
	CAShapeLayer *barPathLayer		= [_barPathLayers objectAtIndex:index];
	barPathLayer.hidden				= NO;
	[self drawBar:barPathLayer];
    
	[CATransaction commit];
}

- (void)drawBar:(CAShapeLayer *)barPathLayer
{
	if (self.animationDuration == 0.0)
		return;
	
	[barPathLayer removeAllAnimations];
    
	CABasicAnimation *pathAnimation	= [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
	pathAnimation.fromValue			= [NSNumber numberWithFloat:0.0f];
	pathAnimation.toValue			= [NSNumber numberWithFloat:1.0f];
	[barPathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

#pragma mark Grid

- (void)setupGrid
{
	if (_gridPathLayer != nil)
	{
		[_gridPathLayer removeFromSuperlayer];
		_gridPathLayer = nil;
	}
	
	CGFloat gridUnit		= _gridLayer.bounds.size.height / _topValue;
	CGFloat gridSeperation	= gridUnit * (CGFloat)self.incrementValue;
    
	CGFloat yPos			= gridSeperation;
	UIBezierPath *path		= [UIBezierPath bezierPath];
	while (yPos <= _gridLayer.bounds.size.height)
	{
		CGPoint left	= CGPointMake(0.0, yPos);
		CGPoint right	= CGPointMake(_gridLayer.bounds.size.width, yPos);
		yPos			+= gridSeperation;
        
		[path moveToPoint:left];
		[path addLineToPoint:right];
	}
    
	_gridPathLayer					= [CAShapeLayer layer];
	_gridPathLayer.frame			= _gridLayer.bounds;
	_gridPathLayer.bounds			= _gridLayer.bounds;
	_gridPathLayer.geometryFlipped	= YES;
	_gridPathLayer.path				= path.CGPath;
	_gridPathLayer.strokeColor		= self.gridColor.CGColor;
	_gridPathLayer.fillColor		= nil;
	_gridPathLayer.lineWidth		= 1.0f;
	_gridPathLayer.lineJoin			= kCALineJoinBevel;
    
	[_gridLayer addSublayer:_gridPathLayer];
}

- (void)drawGrid
{
	if (self.animationDuration == 0.0)
		return;
	
	[_gridPathLayer removeAllAnimations];
    
    CABasicAnimation *pathAnimation	= [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration			= self.animationDuration;
    pathAnimation.fromValue			= [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue			= [NSNumber numberWithFloat:1.0f];
	pathAnimation.timingFunction	= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [_gridPathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}

#pragma mark Axis Labels

- (void)setupYAxisLabels
{
	if (!self.hasYLabels)
		return;
    
	if (_yLabelView.alpha > 0.0)
	{
		_yLabelView.alpha = 0.0;
		[[_yLabelView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
	}
    
	CGFloat gridUnit			= _gridLayer.bounds.size.height / _topValue;
	CGFloat gridSeperation		= gridUnit * (CGFloat)self.incrementValue;
    
    NSDictionary *yLabelFontAttributes = @{NSFontAttributeName: self.yLabelFont};
	CGSize yLabelSize			= [[NSString stringWithFormat:@"%li", (long)_topValue] sizeWithAttributes:yLabelFontAttributes];
	CGFloat yPos				= 0.0;
	NSInteger maxVal			= _topValue;
	while (yPos < _gridLayer.bounds.size.height)
	{
		CGRect yLabelFrame		= CGRectMake(0.0,
											 0.0,
											 yLabelSize.width,
											 yLabelSize.height);
		UILabel *yLabel			= [[UILabel alloc] initWithFrame:yLabelFrame];
		yLabel.font				= self.yLabelFont;
		yLabel.backgroundColor	= [UIColor clearColor];
		yLabel.textColor		= self.yLabelColor;
		yLabel.textAlignment	= NSTextAlignmentRight;
		yLabel.center			= CGPointMake(yLabel.center.x, yPos);
		yLabel.text				= [NSString stringWithFormat:@"%li", (long)maxVal];
        
		[_yLabelView addSubview:yLabel];
        
		maxVal					-= self.incrementValue;
		yPos					+= gridSeperation;
	}
}

- (void)setupXAxisLabels
{
	if (_barLabels.count == 0)
		return;
    
	if (_xLabelView.alpha > 0.0)
	{
		_xLabelView.alpha = 0.0;
		[[_xLabelView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
	}
    
	CGFloat	xPos				= _barLayer.bounds.size.width / (_numberOfBars + 1);
    
    
    XAxisList = [[NSMutableArray alloc] init];
    
	for (NSInteger i = 0; i < _numberOfBars; i++)
	{
		NSString *xLabelText	= [_barLabels objectAtIndex:i];
        
        NSDictionary *xLabelFontAttributes = @{NSFontAttributeName: self.xLabelFont};
		CGSize xLabelSize		= [xLabelText sizeWithAttributes:xLabelFontAttributes];
        //		CGRect xLabelFrame		= CGRectMake(0.0,
        //											 0.0,
        //											 xLabelSize.width,
        //											 xLabelSize.height);
        
        NSDictionary *xBoldLabelFontAttributes = @{NSFontAttributeName: self.xBoldLabelFont};
        CGSize xBoldLabelSize		= [xLabelText sizeWithAttributes:xBoldLabelFontAttributes];
		CGRect xBoldLabelFrame		= CGRectMake(0.0,
                                                 0.0,
                                                 xBoldLabelSize.width,
                                                 xBoldLabelSize.height);
        
		UILabel *xLabel			= [[UILabel alloc] initWithFrame:xBoldLabelFrame];
		xLabel.font				= self.xBoldLabelFont;//self.xLabelFont;
		xLabel.backgroundColor	= [UIColor clearColor];
		xLabel.textColor		= self.xLabelColor;
		xLabel.textAlignment	= NSTextAlignmentNatural;
		xLabel.text				= xLabelText;
		xLabel.transform		= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-_xLabelRotation));
        
        //        //########
        //        UILabel *saXLabel = [[UILabel alloc] initWithFrame:xLabelFrame];
        //        saXLabel.font				= self.xLabelFont;
        //		saXLabel.backgroundColor	= [UIColor clearColor];
        //		saXLabel.textColor		= [UIColor blueColor];
        //		saXLabel.textAlignment	= NSTextAlignmentLeft;
        //		saXLabel.text				= @"SA";
        //		saXLabel.transform		= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-_xLabelRotation));
        //        //########
        //        UILabel *apXLabel = [[UILabel alloc] initWithFrame:xLabelFrame];
        //        apXLabel.font				= self.xLabelFont;
        //		apXLabel.backgroundColor	= [UIColor clearColor];
        //		apXLabel.textColor		= [UIColor blueColor];
        //		apXLabel.textAlignment	= NSTextAlignmentLeft;
        //		apXLabel.text				= @"AP";
        //		apXLabel.transform		= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-_xLabelRotation));
        //        //########
        //        UILabel *pXLabel = [[UILabel alloc] initWithFrame:xLabelFrame];
        //        pXLabel.font				= self.xLabelFont;
        //		pXLabel.backgroundColor	= [UIColor clearColor];
        //		pXLabel.textColor		= [UIColor blueColor];
        //		pXLabel.textAlignment	= NSTextAlignmentLeft;
        //		pXLabel.text				= @"P";
        //		pXLabel.transform		= CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-_xLabelRotation));
        //        //########
        
        
		// Position the label appropriately
		switch (self.xLabelType)
		{
			case SimpleBarChartForSkillXLabelTypeVerticle:
			default:
				xLabel.center = CGPointMake(xPos, (xLabelSize.width / 2.0+15));
                //                saXLabel.center = CGPointMake(xPos-20+i, (xLabelSize.width / 2.0));
                //                apXLabel.center = CGPointMake(xPos+6+i, (xLabelSize.width / 2.0));
                //                pXLabel.center = CGPointMake(xPos+35+i, (xLabelSize.width / 2.0));
				break;
                
			case SimpleBarChartForSkillXLabelTypeHorizontal:
				xLabel.center = CGPointMake(xPos, _xLabelMaxHeight / 2.0+15);
                //                saXLabel.center = CGPointMake(xPos-20+i, _xLabelMaxHeight / 2.0);
                //                apXLabel.center = CGPointMake(xPos+6+i, _xLabelMaxHeight / 2.0);
                //                pXLabel.center = CGPointMake(xPos+35+i, _xLabelMaxHeight / 2.0);
				break;
                
			case SimpleBarChartForSkillXLabelTypeAngled:
			{
				CGFloat labelHeightWithAngle	= sin(DEGREES_TO_RADIANS(_xLabelRotation)) * xLabelSize.width;
				xLabel.center					= CGPointMake(xPos - (labelHeightWithAngle / 2.0), labelHeightWithAngle / 2.0+15);
                //                saXLabel.center					= CGPointMake(xPos-20+i - (labelHeightWithAngle / 2.0), labelHeightWithAngle / 2.0);
                //                apXLabel.center					= CGPointMake(xPos+6+i - (labelHeightWithAngle / 2.0), labelHeightWithAngle / 2.0);
                //                pXLabel.center					= CGPointMake(xPos+35+i - (labelHeightWithAngle / 2.0), labelHeightWithAngle / 2.0);
				break;
			}
		}
        
        [XAxisList addObject:[NSNumber numberWithDouble:xPos]];
        
		[_xLabelView addSubview:xLabel];
        
        //        [_xLabelView addSubview:saXLabel];
        //        [_xLabelView addSubview:apXLabel];
        //        [_xLabelView addSubview:pXLabel	];
        
        
        
        
		xPos					+= _barLayer.bounds.size.width / (_numberOfBars + 1);
	}
}

- (void)setupBarTexts
{
	if (_barTexts.count == 0)
		return;
	
	if (_barTextView.alpha > 0.0)
	{
		_barTextView.alpha = 0.0;
		[[_barTextView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
	}
    
	CGFloat	xPos				= _barLayer.bounds.size.width / (_numberOfBars + 1);
    
	for (NSInteger i = 0; i < _barTexts.count; i++)
	{
        //#################
        
        NSDictionary *barTextFontAttributes = @{NSFontAttributeName: self.barTextFont};
        
		NSString *barSaLabelText	= [_barTexts objectAtIndex:(i)];
        
        
		CGSize barSaTextSize		= [barSaLabelText sizeWithAttributes:barTextFontAttributes];
		CGRect barSaTextFrame		= CGRectMake(0.0,
                                                 0.0,
                                                 barSaTextSize.width,
                                                 barSaTextSize.height);
		UILabel *barSaText		= [[UILabel alloc] initWithFrame:barSaTextFrame];
		barSaText.font			= self.barTextFont;
		barSaText.backgroundColor	= [UIColor clearColor];
		barSaText.textColor		= self.barCountTextColor;
		barSaText.textAlignment	= NSTextAlignmentCenter;
		barSaText.text			= barSaLabelText;
        barSaText.shadowColor   = [UIColor blackColor];
        
        CGFloat barSaHeight		= (_barLayer.bounds.size.height / (CGFloat)_topValue) * ((NSNumber *)[_LegacyRequirementsbarHeights objectAtIndex:i]).floatValue;
        CGFloat minSaBarHeight	= (_barLayer.bounds.size.height / (CGFloat)_topValue) * _minSaHeight.floatValue;
        //#################
		NSString *barApLabelText	= [_barTexts objectAtIndex:(++i)];
        
        
		CGSize barApTextSize		= [barApLabelText sizeWithAttributes:barTextFontAttributes];
		CGRect barApTextFrame		= CGRectMake(0.0,
                                                 0.0,
                                                 barApTextSize.width,
                                                 barApTextSize.height);
		UILabel *barApText		= [[UILabel alloc] initWithFrame:barApTextFrame];
		barApText.font			= self.barTextFont;
		barApText.backgroundColor	= [UIColor clearColor];
		barApText.textColor		= self.barCountTextColor;
		barApText.textAlignment	= NSTextAlignmentCenter;
		barApText.text			= barApLabelText;
        barApText.shadowColor   = [UIColor blackColor];
        
        CGFloat barApHeight		= (_barLayer.bounds.size.height / (CGFloat)_topValue) * ((NSNumber *)[_LegacyRequirementsbarHeights objectAtIndex:(i)]).floatValue;
        CGFloat minApBarHeight	= (_barLayer.bounds.size.height / (CGFloat)_topValue) * _minApHeight.floatValue;
        //#################
		NSString *barPLabelText	= [_barTexts objectAtIndex:(++i)];
        
        
		CGSize barPTextSize		= [barPLabelText sizeWithAttributes:barTextFontAttributes];
		CGRect barPTextFrame		= CGRectMake(0.0,
                                                 0.0,
                                                 barPTextSize.width,
                                                 barPTextSize.height);
		UILabel *barPText		= [[UILabel alloc] initWithFrame:barPTextFrame];
		barPText.font			= self.barTextFont;
		barPText.backgroundColor	= [UIColor clearColor];
		barPText.textColor		= self.barCountTextColor;
		barPText.textAlignment	= NSTextAlignmentCenter;
		barPText.text			= barPLabelText;
        barPText.shadowColor   = [UIColor blackColor];
        
        CGFloat barPHeight		= (_barLayer.bounds.size.height / (CGFloat)_topValue) * ((NSNumber *)[_LegacyRequirementsbarHeights objectAtIndex:i]).floatValue;
        
        CGFloat minPBarHeight	= (_barLayer.bounds.size.height / (CGFloat)_topValue) * _minPHeight.floatValue;
        
        
        //################
        NSDictionary *xLabelFontAttributes = @{NSFontAttributeName: self.barTextFont};
        
        NSString *barSaDescLabelText	= @"SA";
        NSString *barApDescLabelText	= @"AP";
        NSString *barPDescLabelText	= @"P";
        
		CGSize xSaDescLabelSize		= [barSaDescLabelText sizeWithAttributes:xLabelFontAttributes];
        CGSize xApDescLabelSize		= [barApDescLabelText sizeWithAttributes:xLabelFontAttributes];
        CGSize xPDescLabelSize		= [barPDescLabelText sizeWithAttributes:xLabelFontAttributes];
        
        CGRect barSaDescTextFrame		= CGRectMake(0.0,
                                                     0.0,
                                                     xSaDescLabelSize.width,
                                                     xSaDescLabelSize.height);
        CGRect barApDescTextFrame		= CGRectMake(0.0,
                                                     0.0,
                                                     xApDescLabelSize.width,
                                                     xApDescLabelSize.height);
        CGRect barPDescTextFrame		= CGRectMake(0.0,
                                                     0.0,
                                                     xPDescLabelSize.width,
                                                     xPDescLabelSize.height);
        
        
        UILabel *barSaDescText		= [[UILabel alloc] initWithFrame:barSaDescTextFrame];
		barSaDescText.font			= self.barTextFont;
		barSaDescText.backgroundColor	= [UIColor clearColor];
		barSaDescText.textColor		= [UIColor blueColor];
		barSaDescText.textAlignment	= NSTextAlignmentCenter;
		barSaDescText.text			= barSaDescLabelText;
        
        UILabel *barApDescText		= [[UILabel alloc] initWithFrame:barApDescTextFrame];
		barApDescText.font			= self.barTextFont;
		barApDescText.backgroundColor	= [UIColor clearColor];
		barApDescText.textColor		= [UIColor blueColor];
		barApDescText.textAlignment	= NSTextAlignmentCenter;
		barApDescText.text			= barApDescLabelText;
        
        
        UILabel *barPDescText		= [[UILabel alloc] initWithFrame:barPDescTextFrame];
		barPDescText.font			= self.barTextFont;
		barPDescText.backgroundColor	= [UIColor clearColor];
		barPDescText.textColor		= [UIColor blueColor];
		barPDescText.textAlignment	= NSTextAlignmentCenter;
		barPDescText.text			= barPDescLabelText;
        
        
        //################
		
        // Position the label appropriately
		switch (self.barTextType)
		{
			case SimpleBarChartForSkillBarTextTypeTop:
			default:
				barSaText.center        = CGPointMake(xPos-25, _barLayer.bounds.size.height - (barSaHeight - (barSaTextSize.height / 2.0)));
                barSaDescText.center    = CGPointMake(xPos-25, _barLayer.bounds.size.height - (0 - (barSaTextSize.height / 2.0)));
                
                barApText.center        = CGPointMake(xPos, _barLayer.bounds.size.height - (barApHeight - (barApTextSize.height / 2.0)));
                barApDescText.center    = CGPointMake(xPos, _barLayer.bounds.size.height - (0 - (barApTextSize.height / 2.0)));
                
                barPText.center         = CGPointMake(xPos+25, _barLayer.bounds.size.height - (barPHeight - (barPTextSize.height / 2.0)));
                barPDescText.center     = CGPointMake(xPos+25, _barLayer.bounds.size.height - (0 - (barPTextSize.height / 2.0)));
                
				break;
                
			case SimpleBarChartForSkillBarTextTypeRoof:
				barSaText.center        = CGPointMake(xPos-25, _barLayer.bounds.size.height - (barSaHeight + (barSaTextSize.height / 2.0)));
                barSaDescText.center    = CGPointMake(xPos-25, _barLayer.bounds.size.height - (0 + (barSaTextSize.height / 2.0)));
                
                barApText.center        = CGPointMake(xPos, _barLayer.bounds.size.height - (barApHeight + (barApTextSize.height / 2.0)));
                barApDescText.center    = CGPointMake(xPos, _barLayer.bounds.size.height - (0 + (barApTextSize.height / 2.0)));
                
                barPText.center         = CGPointMake(xPos+25, _barLayer.bounds.size.height - (barPHeight + (barPTextSize.height / 2.0)));
                barPDescText.center     = CGPointMake(xPos+25, _barLayer.bounds.size.height - (0 + (barPTextSize.height / 2.0)));
                
				break;
                
			case SimpleBarChartForSkillBarTextTypeMiddle:
			{
				
				barSaText.center        = CGPointMake(xPos-25, _barLayer.bounds.size.height - (minSaBarHeight / 2.0));
                barSaDescText.center    = CGPointMake(xPos-25, _barLayer.bounds.size.height - (minSaBarHeight / 2.0));
                
                barApText.center        = CGPointMake(xPos, _barLayer.bounds.size.height - (minApBarHeight / 2.0));
                barApDescText.center    = CGPointMake(xPos-25, _barLayer.bounds.size.height - (minSaBarHeight / 2.0));
                
                barPText.center         = CGPointMake(xPos+25, _barLayer.bounds.size.height - (minPBarHeight / 2.0));
                barPDescText.center     = CGPointMake(xPos-25, _barLayer.bounds.size.height - (minSaBarHeight / 2.0));
                
				break;
			}
		}
        
        if (![barSaLabelText isEqualToString:@"0"]) {
            [_barTextView addSubview:barSaText];
            
        }
        [_barTextView addSubview:barSaDescText];
        if (![barApLabelText isEqualToString:@"0"]) {
            [_barTextView addSubview:barApText];
            
        }
        [_barTextView addSubview:barApDescText];
        if (![barPLabelText isEqualToString:@"0"]) {
            [_barTextView addSubview:barPText];
            
        }
        [_barTextView addSubview:barPDescText];
		
        
		xPos += _barLayer.bounds.size.width / (_numberOfBars + 1);
	}
}

- (void)displayAxisLabels
{
	if (self.hasYLabels || _barTexts.count > 0 || _barLabels.count > 0)
	{
		if (self.animationDuration > 0.0)
		{
			__weak SimpleBarChartForSkill *weakSelf = self;
			[UIView animateWithDuration:self.animationDuration / 2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
				_yLabelView.alpha	= 1.0;
				_xLabelView.alpha	= 1.0;
				_barTextView.alpha	= 1.0;
			} completion:^(BOOL finished) {
				if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(animationDidEndForBarChart:)])
					[weakSelf.delegate animationDidEndForBarChart:weakSelf];
			}];
		}
		else
		{
			_yLabelView.alpha	= 1.0;
			_xLabelView.alpha	= 1.0;
			_barTextView.alpha	= 1.0;
			
			if (_delegate && [_delegate respondsToSelector:@selector(animationDidEndForBarChart:)])
				[_delegate animationDidEndForBarChart:self];
		}
	}
	else
	{
		if (_delegate && [_delegate respondsToSelector:@selector(animationDidEndForBarChart:)])
			[_delegate animationDidEndForBarChart:self];
	}
}

@end
